import { getData } from './dataStore'

const MIN_NAME_LENGTH = 2;
const MAX_NAME_LENGTH = 20;
const MIN_PASSWORD_LENGTH = 8;

export function isValidNameContains(name: string): boolean {
    const validNameRegex = /^[a-zA-Z\s\-']+$/;
    return validNameRegex.test(name);
}

export function isValidNameLength(name: string): boolean {
    return name.length >= MIN_NAME_LENGTH && name.length <= MAX_NAME_LENGTH;
}
  
export function isValidPasswordLength(password: string): boolean {
    return password.length >= MIN_PASSWORD_LENGTH;
}

export function isValidPasswordContains(password: string): boolean {
    const containsNumber = /\d/;
    const containsLetter = /[a-zA-Z]/;

    return containsNumber.test(password) && containsLetter.test(password);
}

export function isValidToken(sessionId: string): number | null {
    const data = getData();
    for (const [userId, sessionIds] of Object.entries(data.Tokens)) {
        if ((sessionIds as string[]).includes(sessionId)) {
            return parseInt(userId);
      }
    }
    return null;
}